<?php
namespace Common\Model;
use Think\Model;

class CommentModel extends Model{

	protected $trueTableName = 'comment';



	protected $_validate = array(
		array('content','require','评论内容不能为空'),
		array('info_id','require','id不能为空'),
	);

	public function getComByInfoId($infoId){
		$map['info_id'] = array('eq',$infoId);
		$map['status'] = array('eq',1);
		$res =$this->where($map)->field("content,ip,create_time")->order('create_time desc')->limit(10)->select();
		return $res;
	}

	public function getList(){
		$p = I('p',0,'int');
		$limit = C('DEFALUT_PAGE_SIZE');
		$startNum = $p * $limit;
		$count = $this->field("count(*) as count")->find();
		$data = $this->limit($startNum,$limit)->select();
		return array(
			'count' => $count['count'],
			'data'  => $data
		);
	}

	public function addComment(){
		$post = A('post');
		$val = $post->CheckCode(I('validate'));
		if(!$val){
			$this->error = "验证码错误";
			return false;
		}
		if (!$this->create()){
			return false;
		}else{

			$data['status']			= C('cfg_comment_verify');
			$data['info_id'] 		= (int)I("info_id");
			$data['content'] 		= mb_substr(str_replace(C('cfg_replace_word'),'***',I('content')),0,255,'utf8');
			$data['ip']				= get_client_ip();
			$user = cookie(C('COOK_LOGIN_KEY')) ? explode('_',cookie(C('COOK_LOGIN_KEY')))[0] : "";
			$data['user_id']		= $user;

			$this->add($data);
			return true;
		}
	}

	public function pass(){
		$id = I('get.id');
		$idArr = array_filter(explode(',',$id));
		$idArr =  array_map('intval', $idArr);
		$data['status'] = 1;
		$map['id'] = array('in',$idArr);
		$res = $this->where($map)->save($data);
		return $res;
	}

	public function del(){
		$id = I('get.id');
		$idArr = array_filter(explode(',',$id));
		$idArr =  array_map('intval', $idArr);
		$map['id'] = array('in',$idArr);
		$res = $this->where($map)->delete();
		return $res;
	}

}
